package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 工作台业务数据
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //可以优化不用创建那么多map，map中的健是唯一的新设置的值会覆盖旧值，基础只是，同时这里的全部数据都应该使用的当天时间的
//        Map orderMap = new HashMap<>();
//        orderMap.put("status", Orders.COMPLETED);
//        Double turnover = ordersMapper.findByTime(orderMap);
//        Integer validOrderCount = ordersMapper.getOrdersStatistics(orderMap);
//        Integer totalOrderCount = ordersMapper.getOrdersStatistics(new HashMap<>());
//        Double orderCompletionRate = (double)validOrderCount/totalOrderCount;
//        Double unitPrice = turnover / validOrderCount;
//        // 使用 DecimalFormat 保留两位小数
//        DecimalFormat df = new DecimalFormat("#.00");
//        String formattedUnitPrice = df.format(unitPrice);
//
//        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
//        LocalDateTime end = LocalDateTime.of(LocalDate.now(),LocalTime.MAX);
//        Map userMap = new HashMap<>();
//        userMap.put("begin",begin);
//        userMap.put("end",end);
//        Integer newUser = userMapper.findByMap(userMap);
//        return BusinessDataVO
//                .builder()
//                .turnover(turnover)
//                .newUsers(newUser)
//                .orderCompletionRate(orderCompletionRate)
//                .unitPrice(Double.parseDouble(formattedUnitPrice))
//                .validOrderCount(validOrderCount)
//                .build();

        //修改后
        Map orderMap = new HashMap<>();
        orderMap.put("begin",begin);
        orderMap.put("end",end);
        Integer newUser = userMapper.findByMap(orderMap);
        orderMap.put("status",Orders.COMPLETED);
        Double turnover = ordersMapper.findByTime(orderMap);
        turnover = turnover == null ? 0.0 : turnover;
        Integer validOrderCount = ordersMapper.getOrdersStatistics(orderMap);
        orderMap.put("status",null);
        Integer totalOrderCount = ordersMapper.getOrdersStatistics(orderMap);
        Double orderCompletionRate = totalOrderCount == 0 ? 0.0 : (double)validOrderCount/totalOrderCount;
        Double unitPrice = validOrderCount == 0 ? 0.0 : turnover / validOrderCount;
        // 使用 DecimalFormat 保留两位小数
        DecimalFormat df = new DecimalFormat("#.00");
        String formattedUnitPrice = df.format(unitPrice);

        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .newUsers(newUser)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(Double.parseDouble(formattedUnitPrice))
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 订单管理数据
     * @return
     */
    public OrderOverViewVO getOverviewOrders(LocalDateTime begin, LocalDateTime end) {

        //待接单数量
        Map orderMap = new HashMap<>();
        orderMap.put("begin",begin);
        orderMap.put("end",end);
        orderMap.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.getOrdersStatistics(orderMap);

        //待派送数量
        orderMap.put("status",Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = ordersMapper.getOrdersStatistics(orderMap);

        //已完成数量
        orderMap.put("status",Orders.COMPLETED);
        Integer completedOrders = ordersMapper.getOrdersStatistics(orderMap);

        //已取消数量
        orderMap.put("status",Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.getOrdersStatistics(orderMap);
        orderMap.put("status",null);
        //全部订单
        Integer allOrders = ordersMapper.getOrdersStatistics(orderMap);
        return OrderOverViewVO
                .builder()
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 菜品管理数据
     * @return
     */
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.getCountByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.getCountByStatus(StatusConstant.DISABLE);
        return DishOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 套餐管理数据
     * @return
     */
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setmealMapper.getCountByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.getCountByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
